package com.naiterui.ehp.bs.wechat.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.wechat.constants.WeChatConstant;
import com.naiterui.ehp.bs.wechat.service.IMediaApiService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 微信多媒体文件配置
 *
 * @author yangfuya
 * @since 2021/8/10
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WechatMediaConfig implements InitializingBean {

    private final IMediaApiService mediaApiService;

    @Override
    public void afterPropertiesSet() {
        try {
            // 判断视频问诊的客户消息图片是否已经上传
            if (!RedisUtil.keyOps().existsKey(CommonConstant.VIDEO_CONSULT_THUMB_MEDIA_CACHE_KEY)) {
                log.info("上传视频问诊客服消息图片");
                String mediaId = mediaApiService.uploadMedia(WeChatConstant.MediaFileType.THUMB,
                    ConfigUtil.getString("video-consult.thumb-media.path"));
                log.info("WechatMediaConfig Initializing Result:{} " + mediaId);
                RedisUtil.valueOps().set(CommonConstant.VIDEO_CONSULT_THUMB_MEDIA_CACHE_KEY, mediaId);
            }
        } catch (Exception e) {
            log.warn("WechatMediaConfig Initializing ERROR ", e);
        }
    }
}